
uniform sampler2D sBaseMap;
uniform sampler2D sBumpMap;

varying vec3 vViewVec;
varying vec3 vLightVec;
varying vec2 vTexCoord;

void main()
{
	// Fetch basemap color
	vec4 baseColor = texture2D( sBaseMap, vTexCoord );
	
	// Fetch the tangent space normal from normal map
	vec3 normal = texture2D( sBumpMap, vTexCoord ).xyz;
	// Scale and bias from [0, 1] to [-1, 1] and normalize
	normal = normalize( normal * 2.0 - 1.0 );
	
	// Normalize the light direction and view direction
	vec3 lightDirection = normalize( vLightVec );
	vec3 viewDirection = normalize( vViewVec );
	
	// Compute N.L
	float nDotL = dot( normal, lightDirection );
	
	// Compute reflection vector
	vec3 reflection = ( 2.0 * normal * nDotL ) - lightDirection;
	
	// Compute R.V
	float rDotV = max( 0.0, dot( reflection, viewDirection ) );
	
	// Compute Ambient term
	vec4 ambient = vec4(0.368,0.368,0.368,1.0) * baseColor;
	
	// Compute Diffuse term
	vec4 diffuse = vec4(0.885,0.885,0.885,1.0) * nDotL * baseColor;
	
	// Compute Specular term
	vec4 specular = vec4(0.488,0.488,0.488,1.0) * pow( rDotV, 25.0 );
	
	// Output final color
	gl_FragColor = ambient + diffuse + specular;
}


